<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Auth for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Auth;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Auth\Model\ListMapper;
use Auth\Model\SettingMapper;
use Auth\Model\UserMapper;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
		    // if we're in a namespace deeper than one level we need to fix the \ in the path
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Auth\Model\ListMapper' => function($sm) {
                    $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $listMapper = new ListMapper($adapter);
                    return $listMapper;
                },
                'Auth\Model\SettingMapper' => function($sm) {
                $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                $settingMapper = new SettingMapper($adapter);
                return $settingMapper;
                },
                'Auth\Model\UserMapper' => function($sm) {
                $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                $userMapper = new UserMapper($adapter);
                return $userMapper;
                },
                "FEAuthStorage" => function ($sm){
                    $storage = new \Auth\Model\Session\AuthSession("FE_Auth");
                    return $storage;
                },
                // save session luu du thong tin user
                
                'FEAuthService' => function ($sm){
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users', 'email', 'password', 'hash');
                     
                    $authService = new AuthenticationService();// tao doi auth sevice
                    $authService->setAdapter($dbTableAuthAdapter);// ket noi database
                    $authService->setStorage($sm->get('FEAuthStorage')); //luu vao session
                     
                    return $authService;
                },
                // tao doi tuong kiem tra dang nhap
            )
        );        
    }
}
